/*
 * @Author: Broin
 * @Date: 2021-12-29 02:10:34
 * @LastEditTime: 2022-01-04 08:08:05
 * @LastEditors: your name
 * @Description: 
 * @FilePath: /ac_interface/src/api/users.js
 * MADE BY BROIN
 */

import User from '../utils/roles.js';
import { users } from '../data/users.js';


/**
 * @description 添加一个用户
 * @param {*} username 
 * @param {*} pRole 
 * @param {*} normalpass 
 * @param {*} rootpass 
 */
export function addUser(username, pRole, normalpass, rootpass) {
    users[users.length] = new User(username, pRole, normalpass, rootpass);
    return 0;
}


/**
 * @description 删除一个用户
 * @param {} username 
 */
export function delUser(username) {
    let j;
    let hasUser = false;
    for(j = 0; j < users.length; j++){
        if(users[j].username == username){
            hasUser = true;
            let temp = users[users.length - 1];
            users[users.length - 1] = users[j];
            users[j] = temp;
        }
    }
    if(hasUser){
        users.pop();
        return 0;
    }
    return -1;
}

/**
 * @description 通过用户名查找一个用户
 * @param {} username 
 * @returns 
 */
export function findUser(username) {
    for (let i = 0; i < users.length; i++) {
        if (users[i].username == username) {
            return i;
        }      
    }
    
    alert("没有这个用户");
    return -1;
}

/**
 * @description 用户登录
 * @param {} username 
 * @param {*} pass 
 * @returns 
 */
export function login(username, pass) {

    let index = findUser(username);
    if(index == -1){
        return {
            username: username,
            password: pass,
            code: -1
        };
    }
    if(users[index].pRole == 'admin'){
        return {
            username: username,
            password: pass,
            code: 1
        };
    }
    console.log(users[index].normalpass);
    if(users[index].normalpass == pass){
        users[index].vmRole = 'normal';
    }
    if (users[index].rootpass == pass) {
        users[index].vmRole = 'root';
    }
    if ( users[index].normalpass != pass && users[index].rootpass != pass) {
        console.log('没有这个用户');
        alert('用户名或密码错误');
        return {
            username: username,
            password: pass,
            vmRole: null, 
            code : -1
        };
    }

    return {
        username: username,
        password: pass,
        vmRole: users[index].vmRole,
        code: 0
    };
}

/**
 * @description 查找该用户可访问的虚拟机
 * @param {} username 该用户名
 * @returns 
 */
export function getMachines(username){
    let index = findUser(username);
    return {
        machines : users[index].machines,
        machinesNum : users[index].machinesNum,
        code : 1
    }
}


/**
 * @description 通过用户名得到平台角色
 * @param {} username 
 * @returns pRole,用户平台角色
 */
export function getpRole(username){
    let index = findUser(username);
    if (index != -1){
        return {
            pRole: users[index].pRole,
            code: 0
        }
        
    }
    else{
        return {
            pRole: user[index].pRole,
            code: -1
        };
    }
}

/**
 * @description 列出所有角色名
 * @returns 
 */
export function listUser(){
    let list = [];
    for (let i = 0; i < users.length; i++) {
        list.push(users[i].username);
    }
    return {
        list: list,
        code: 0
    }
}
